package com.ysm.smfu.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.ysm.smfu.R;
import com.ysm.smfu.adapter.DefaultAdapter;
import com.ysm.smfu.bean.Bonus;
import com.ysm.smfu.holder.BaseHolder;
import com.ysm.smfu.holder.BonusHolder;
import com.ysm.smfu.protocal.BaseProtocal;
import com.ysm.smfu.protocal.MyStringCallBack;
import com.ysm.smfu.utils.JsonUtil;
import com.ysm.smfu.utils.PrintUtil;
import com.ysm.smfu.view.RefreshLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class BonusDetailActivity extends BaseRefreshActivity<Bonus.Bonu> {
    @BindView(R.id.title_body)
    TextView titleBody;
    @BindView(R.id.mainActivity_topbar_img)
    ImageView mainActivityTopbarImg;
    @BindView(R.id.myBonus)
    TextView myBonus;
    @BindView(R.id.myAllBonus)
    TextView myAllBonus;

    @BindView(R.id.listView_dingdanDetail)
    ListView listViewDingdanDetail;
    @BindView(R.id.refresh)
    RefreshLayout refresh;
    @BindView(R.id.noBonus)
    TextView noBonus;
    private BonusDetailAdapter adapter;
    private String state;

    @Override
    protected void initViews() {
        super.initViews();
        setContentView(R.layout.activity_bonus_detail);
        ButterKnife.bind(this);
        initRefresh(refresh, true);
        titleBody.setText("奖金明细");
        mainActivityTopbarImg.setVisibility(View.GONE);
    }


    @Override
    protected void initData() {
        super.initData();
        adapter = new BonusDetailAdapter(datas, listViewDingdanDetail, null);
        listViewDingdanDetail.setAdapter(adapter);
        getBonusDetail();
    }

    @Override
    public void load(boolean isLoad) {
        if (isLoad)
            page++;
        getBonusDetail();
    }

    private class BonusDetailAdapter extends DefaultAdapter<Bonus.Bonu> {

        public BonusDetailAdapter(List<Bonus.Bonu> datas, ListView listView, Fragment fragment) {
            super(datas, listView, fragment);
        }

        @Override
        protected BaseHolder<Bonus.Bonu> getHolder() {
            return new BonusHolder();
        }
    }

    /**
     * 调用接口获取奖金明细
     * app/getBonus
     */
    public void getBonusDetail() {
        BaseProtocal.builder(false).getBonusDetail(new MyStringCallBack() {
            @Override
            public void onSuccess(String response) {
                PrintUtil.printRespones("页码:" + page + "佣金详情", response);
                setPage(response);
            }

            @Override
            public void onFail(Exception e) {
                noData();
            }
        }, page);
    }

    private void setPage(String response) {
        state = JsonUtil.getState(response);
        Bonus bonus = new Gson().fromJson(response, Bonus.class);
        Bonus.Bonus_Account account = bonus.account;
        if (TextUtils.equals(state, "1")) {
            if (account != null) {
//            if (!TextUtils.equals(account.balance, "0.00"))
                myBonus.setText("￥" + account.balance);
//            if (!TextUtils.equals(account.bonus, "0.00"))
                myAllBonus.setText("￥" + account.bonus);
                List<Bonus.Bonu> bonusMap = bonus.bonusMap;
                setLoadData(bonusMap);
                adapter.notifyDataSetChanged();
                dealVisibility();
            } else {
                myBonus.setText("￥ 0.00");
                myAllBonus.setText("￥ 0.00");
                noData();
            }
        } else {
            noData();
            if (!TextUtils.equals(JsonUtil.getMessage(response), "未产生奖金"))
                showErrorDialog(BonusDetailActivity.this, JsonUtil.getMessage(response));
        }
    }

    private void dealVisibility() {
        if (datas.size() == 0) {
            refresh.setVisibility(View.GONE);
            noBonus.setVisibility(View.VISIBLE);
        } else {
            refresh.setVisibility(View.VISIBLE);
            noBonus.setVisibility(View.GONE);
        }
    }

    private void noData() {
        onRefreshError();
        if (datas.size() == 0) {
            noBonus.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 提示对话框--没有点击事件
     *
     * @param activity
     * @param msg
     */
    public void showErrorDialog(FragmentActivity activity, String msg) {
        AlertDialog dialog = new AlertDialog.Builder(activity).setMessage(msg)
                .setNegativeButton("确定", null).create();
        dialog.show();
    }
}
